<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.pointExchangeName" clearable class="filter-item" style="width: 200px;" :placeholder="$t('请输入积分兑换名称')"/>

      <el-button class="filter-item" type="info" icon="el-icon-search" @click="handleFilter" v-permission="['GET /admin/p/point_exchange_index']">{{$t('Search')}}</el-button>
      <el-button  class="filter-item"  type="warning" plain1 icon="el-icon-circle-plus" v-permission="['GET /admin/p/point_exchange_add']" @click="handleCreate">{{$t('Add')}}</el-button>
      <!--      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">{{$t('Find')}}</el-button>-->
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" :header-cell-style="{ background: '#f3f6fd', color: '#555' }"
              :element-loading-text="$t('Searching')" border fit highlight-current-row>
      <el-table-column type="index" :index="getIndex" width="50"></el-table-column>

<!--      <el-table-column align="center" :label="$t('订阅_ID')" prop="id" sortable/>-->

      <el-table-column align="center" :label="$t('积分兑换名称')" prop="name"width="200"/>

      <el-table-column align="center" :label="$t('积分需求')" prop="point"/>

      <el-table-column align="center" :label="$t('商品类别')" prop="goodsCategory" width="300"/>
      <el-table-column align="center" :label="$t('三级类别')" prop="categoryList" width="300">
        <template slot-scope="scope">
          <div>
            <el-tag
              v-for="(cat, index) in scope.row.categoryList"
              :key="cat.id"
              size="mini"
              style="margin: 2px"
            >
              {{index+1}}、{{ cat.name }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('金额范围')" prop="moneyRange" width="200"/>

      <el-table-column align="center" :label="$t('状态')" prop="status">
        <template slot-scope="scope">
          <el-tag>
            {{filterStatus(scope.row.status)}}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" fixed="right" :label="$t('Operate')" width="300" class-name="small-padding fixed-width">
        <template slot="header" slot-scope="{row}">
          {{$t('Operate')}}
          <i class="el-icon-setting"style="color: #d35e32"></i>
        </template>
        <template slot-scope="scope">
<!--          <el-button v-permission="['GET /admin/p/elec_coupon_detail']" type="primary" plain1 size="mini" @click="handleDetail(scope.row)">{{$t('Details')}}</el-button>-->
          <el-link v-permission="['GET /admin/p/point_exchange_detail']"  type="primary" plain1 size="mini" @click="handleUpdate(scope.row)">{{scope.row.status == 2 ? $t('编辑'):$t('查看')}}</el-link>
          <el-link v-permission="['GET /admin/p/point_exchange_status']"  type="primary"  plain1 size="mini" @click="handleStatus(scope.row)">{{formateStatus(scope.row.status)}}</el-link>
          <el-link v-show="scope.row.status == 2" v-permission="['GET /admin/p/point_exchange_delete']" type="danger" size="mini" @click="handleDelete(scope.row)">{{$t('Delete')}}</el-link>
<!--          <el-button plain1 size="mini" @click="handleStatistic(scope.row)">{{$t('统计')}}</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->

  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>

<script>
import { listLumiereSubscribe } from '@/api/lumiereSubscribe'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { uploadPath } from '../../api/storage'
import { getToken } from '../../utils/auth'
import { deletePointExchange, pointExchangeList, updateStatus } from '../../api/exchange'


export default {
  name: 'ExchangePoint',
  components: { Pagination },
  filters: {},
  data() {
    const defaultTypeOptions = [
      {
        label: this.$t('订阅设置'),
        value: 1
      }

    ]

    const defaultStatusOptions = [
      {
        label: this.$t('停用'),
        value: 0
      },
      {
        label: this.$t('启用'),
        value: 1
      },
      {
        label: this.$t('新增'),
        value: 2
      }
    ]

    return {
      uploadPath,
      isLevelMember: false,
      isMoneyMember: false,
      defaultTypeOptions,
      defaultStatusOptions,
      typeOptions: Object.assign({}, defaultTypeOptions),
      statusOptions: Object.assign({}, defaultStatusOptions),
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pointExchangeName: undefined,
        page: 1,
        limit: 20,
        sort: 'add_time',
        order: 'desc'
      },

      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: this.$t('Edit'),
        create: this.$t('Create')
      },
      rules: {
        name: [
          { required: true, message: this.$t('Coupon_title_cannot_be_empty'), trigger: 'blur' }
        ],

      },
      downloadLoading: false,

      props: { multiple: true, value: 'id', label: 'name',emitPath:false},
      regionProps: {multiple: true, value: 'id', label: 'nameEn',emitPath:false},
      subscribeSpecifications: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    filterStatus(status){
      if(status == 2){
        return this.$t("下架")
      }else if(status == 1){
        return this.$t("上架")
      }
    },
    getList() {
      this.listLoading = true
      pointExchangeList(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    getIndex(index) {
      return (index + 1) + (this.listQuery.page - 1) * this.listQuery.limit;
    },
    handleCreate() {
      this.dialogStatus = 'create'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })

      this.$router.push({ name: 'pointExchangeAdd', query: { dialogStatus : this.dialogStatus} })
    },

   handleUpdate(row) {
     this.dialogStatus = 'update'
     row.minMoney = row.moneyRange.split("--")[0];
     row.maxMoney = row.moneyRange.split("--")[1];
     this.$router.push({ name: 'pointExchangeAdd', query: { dataForm : row, dialogStatus : this.dialogStatus} })
   },
    formateStatus(status){
      if (status === 1) {
        return this.$t('下架')
      } else if (status === 2) {
        return this.$t('上架')
      }
    },
    handleDelete(row) {
      this.$confirm(this.$t('Confirm_Delete?'), this.$t('Hint'), {
        confirmButtonText: this.$t('Confirm'),
        cancelButtonText: this.$t('Cancel'),
        type: 'warning'
      }).then(() => {
        this.doDelete(row)
      });
    },
    doDelete(row) {
      deletePointExchange(row.id)
        .then(response => {
          this.$notify.success({
            title: this.$t('Success!'),
            message: this.$t('积分兑换删除成功')
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
        .catch(response => {
          this.$notify.error({
            title: this.$t('Failed'),
            message: this.$t(response.data.errmsg)
          })
        })
    },
    handleStatus(row){
      this.$confirm(this.$t('是否') + this.$t('改变该积分兑换状态') + "?", this.$t('提示'), {
        confirmButtonText: this.$t('Confirm'),
        cancelButtonText: this.$t('Cancel'),
        type: this.$t('warning')
      }).then(() => {
        this.doIgnore(row)
      });
    },
    doIgnore(row){
      let data = {
        id: row.id
      }
      updateStatus(row.id).then(response => {
        this.$notify.success({
          title: this.$t('Success!'),
          message: this.$t('Success!')
        })
        this.getList()
      }).catch(response => {
        this.$notify.error({
          title: this.$t('Failed'),
          message: this.$t(response.data.errmsg)
        })
      })
    },
  },

  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  }

}
</script>
